package TestDemo1;

class Animal{
    String name;
    int age;
    public void eat() {
        System.out.println(name+"吃饭");
    }
    public void sleep() {
        System.out.println(name+"睡觉");
    }

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println(name+" 吃猫粮！！！");
    }
    public Cat(String name, int age) {
        super(name, age);
    }

    public void miaomiao() {
        System.out.println(name+" 喵喵~");
    }
}
class Dog extends Animal {
    public Dog(String name, int age) {
        super(name, age);
    }

    public void eat() {

        System.out.println(name+" 吃狗粮！！！");
    }
    public void wangwang() {
        System.out.println(name+" 旺旺！！");
    }

}
public class Test1 {

    public static void main(String[] args) {

    }
    public static void main2(String[] args) {
        Animal animal=new Dog("豆豆",2);
        animal.eat();
        Animal animal1=new Cat("胖胖",3);
        animal1.eat();
    }
    public static void main1(String[] args) {

        Cat cat=new Cat("胖胖",3);
        Dog dog=new Dog("豆豆",2);
        Animal animal1=cat;//向上转型
        Animal animal2=dog;//向上转型
        //安全，因为是猫类向上转型又向下转型类猫类
        cat=(Cat) animal1;
        //运行出错，狗类向上转型为父类，再向下转型为猫类
        if(animal2 instanceof Cat) {
            cat=(Cat) animal2;

        }
         //cat=(Cat) animal2;//
        //狗类不能转型为猫类
    }
}
